﻿namespace TimeSpan.Views.Dialogs;

public partial class NewRecordWindowViewModel : ObservableObject {

    [ObservableProperty]
    private int? fromHour = null;

    [ObservableProperty]
    private int? fromMinute = null;

    [ObservableProperty]
    private int? toHour = null;

    [ObservableProperty]
    private int? toMinute = null;

    [ObservableProperty]
    private string _title = string.Empty;

    [ObservableProperty]
    private int? _presetDuration = null;

    [RelayCommand]
    private void PresetTotal(string? durationText) {
        PresetDuration = durationText switch {
            "60" => 60,
            "90" => 90,
            "120" => 120,
            "150" => 150,
            _ => (int?)null,
        };
    }

    public SpanTaskOption Slice(double spanLevel) {

        if (string.IsNullOrEmpty(Title)) { throw new("请输入任务名称"); }

        var total = PresetDuration ?? 0;
        if (PresetDuration is null) {
            static bool CheckInt(int? value, int max) => value is not null && !(value < 0) && !(value > max);
            if (!CheckInt(FromHour, 23)) { throw new("非法的时间范围"); }
            if (!CheckInt(ToHour, 23)) { throw new("非法的时间范围"); }
            if (!CheckInt(FromMinute, 59)) { throw new("非法的时间范围"); }
            if (!CheckInt(ToMinute, 59)) { throw new("非法的时间范围"); }
            total = ToHour!.Value * 60 + ToMinute!.Value - FromHour!.Value * 60 - FromMinute!.Value;
            if (total < 25 || total > 180) { throw new("时间跨度不合理，请输入总时长在 25 ≤ min ≤ 180 区间内的时间段"); }
        }

        var span = spanLevel switch {
            1 => 25,
            2 => 30,
            3 => 35,
            4 => 40,
            5 => 45,
            6 => 60,
            _ => throw new()
        };

        return new SpanTaskOption() { Title = Title, BlockDuration = span, TotalDuration = total };
    }

}